import {
  getAmountListData,
  getGoodsAddressSale,
  getGoodsCategoryCount,
  getGoodsCategoryFavor,
  getGoodsCategorySale
} from '@/services/main/analysis/analysis'
import { defineStore } from 'pinia'
import type { IStore } from '.'

const useAnalysisStore = defineStore('analysis', {
  state: (): IStore => ({
    amountList: [],
    goodsCategoryCount: [],
    goodsCategorySale: [],
    goodsCategoryFavor: [],
    goodsAddressSale: []
  }),
  actions: {
    fetchAnalysisDataAction() {
      getAmountListData()
        .then((res) => {
          if (res.code === 0) {
            this.amountList = res.data
          } else {
            Promise.reject(new Error('获取商品统计列表数据失败'))
          }
        })
        .catch((error) => Promise.reject(new Error(error.message)))

      getGoodsCategoryCount()
        .then((res) => {
          if (res.code === 0) {
            this.goodsCategoryCount = res.data
          } else {
            Promise.reject(new Error('获取商品统计饼图图标数据失败'))
          }
        })
        .catch((error) => Promise.reject(new Error(error.message)))

      getGoodsCategorySale()
        .then((res) => {
          if (res.code === 0) {
            this.goodsCategorySale = res.data
          } else {
            Promise.reject(new Error('获取商品统计玫瑰图标数据失败'))
          }
        })
        .catch((error) => Promise.reject(new Error(error.message)))

      getGoodsCategoryFavor()
        .then((res) => {
          if (res.code === 0) {
            this.goodsCategoryFavor = res.data
          } else {
            Promise.reject(new Error('获取商品统计柱状图标数据失败'))
          }
        })
        .catch((error) => Promise.reject(new Error(error.message)))

      getGoodsAddressSale()
        .then((res) => {
          if (res.code === 0) {
            this.goodsAddressSale = res.data
          } else {
            Promise.reject(new Error('获取商品地图标数据失败'))
          }
        })
        .catch((error) => Promise.reject(new Error(error.message)))
    }
  }
})

export default useAnalysisStore
